/*
 * Copyright (C) 2011-present ShenZhen iBOXCHAIN Information Technology Co.,Ltd.
 *
 * All right reserved.
 *
 * This software is the confidential and proprietary
 * information of iBOXCHAIN Company of China.
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement
 * you entered into with iBOXCHAIN inc.
 */
package com.example.wac.shiro;

import com.example.wac.constant.AuthConstant;
import com.example.wac.entity.system.ShiroBaseFilter;
import com.example.wac.service.AuthInfoService;
import com.example.wac.service.ShiroBaseFilterService;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuancheng
 * @description
 * @date 2023-04-13 15:22:00
 */

@Component
public class ReloadUrlPermsByDBHelper extends AbstractLoadUrlPerms {

    @Autowired
    private AuthInfoService authInfoService;

    @Autowired
    private ShiroBaseFilterService shiroBaseFilterService;

    @Override
    protected Map<String, Map<String, String>> getServiceToFilterChainMap() {
        // 获取公共过滤链
        List<ShiroBaseFilter> shiroBaseList = shiroBaseFilterService.findListByServiceId();
        Map<String, String> shiroBaseFilterMap = new LinkedHashMap<>();
        shiroBaseList.forEach(shiroBase -> {
            shiroBaseFilterMap.put(shiroBase.getUrl(), shiroBase.getPerms());
        });

        Map<String, Map<String, String>> serviceToFilterChainMap = new LinkedHashMap<>();
        serviceToFilterChainMap.put(AuthConstant.BASE_FILTER, shiroBaseFilterMap);
        // 获取所有菜单权限
        List<Map<String, Object>> authList = authInfoService.listAuthInfo();
        for (Map<String, Object> authInfo : authList) {
            String url = MapUtils.getString(authInfo, "resUrl");
            if (StringUtils.isNotBlank(url)) {
                String httpMethod = MapUtils.getString(authInfo, "httpMethod");
                if (StringUtils.isNotBlank(httpMethod)) {
                    url += ("@@" + httpMethod);
                }
                String perms = "perms[" + MapUtils.getString(authInfo, "authCode") + "]";

                String resModel = MapUtils.getString(authInfo, "resModel");
                Map<String, String> filterChainMap;
                if (null == serviceToFilterChainMap.get(resModel)) {
                    filterChainMap = new LinkedHashMap<>();
                    filterChainMap.put(url, perms);
                    serviceToFilterChainMap.put(resModel, filterChainMap);
                } else {
                    filterChainMap = serviceToFilterChainMap.get(resModel);
                    filterChainMap.put(url, perms);
                }
            }
        }

        this.setServiceToFilterChainToRedis(serviceToFilterChainMap);
        return serviceToFilterChainMap;
    }

}

